package com.woniuxy.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.PaperItem;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.impl.PaperItemServiceImpl;
import com.woniuxy.service.impl.TestServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: Online-education-examination-platform-back
 * @BelongsPackage: com.woniuxy.controller
 * @Author: Banol Zi
 * @CreateTime: 2022-06-08  10:47
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/PaperTest")
public class PaperTestController {

    @Autowired
    private TestServiceImpl testService;

    @Autowired
    private PaperItemServiceImpl paperItemService;

    @GetMapping("/test")
    public ResponseEntity testFindAll(){
        List<PaperItem> paperItems = testService.paperFindAllTest();
        return new ResponseEntity(paperItems);
    }

    @GetMapping("/getPaperById/{id}")
    public ResponseEntity getPaperById(@PathVariable Integer id){
        PaperItem paperItem = paperItemService.getPaperById(id);
        if (paperItem != null){
            return new ResponseEntity(paperItem);
        }else {
            return ResponseEntity.ERROR;
        }
    }

    @PostMapping("/testGene")
    public ResponseEntity testGene(@RequestBody Map<String,Object> postMap){
        paperItemService.getPostData(postMap);
        Integer flag = paperItemService.savePaperDetail();
        if (flag > 0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }

    @PostMapping("/testResult")
    public ResponseEntity resultGet(@RequestBody Map<String,Object> postMap){
        paperItemService.getPostData(postMap);
        return ResponseEntity.SUCCESS;
    }

    @RequestMapping("findPaperItem/{cid}")
    public ResponseEntity findPaperItemByCategoryId(@PathVariable int cid) {
        return new ResponseEntity(paperItemService.findPaperItemByCategoryId(cid));
    }

    @RequestMapping("/findallP/{currentPage}/{pageSize}")
    public ResponseEntity findAllQ(@PathVariable int currentPage,@PathVariable int pageSize, @RequestBody String jsonString){
        //初始化hashmap
        Map<String, Object> map = new HashMap<>();
        //将json字符串转换为json对象
        JSONObject json = JSONObject.parseObject(jsonString);
        //将json对象循环转换为map
        Iterator it = json.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            if (entry.getValue().toString().length() == 0 || entry.getValue().toString().equals("[]")){
                map.put(entry.getKey(), null);
            }else {
                map.put(entry.getKey(), entry.getValue());
                //利用工具类将数组的中括号去掉
                if(entry.getKey().toString().equals("ids")){
                    map.put(entry.getKey(), StringUtils.strip(entry.getValue().toString(),"[]"));
                }
            }
        }
        PageHelper.startPage(currentPage,pageSize);
        List<PaperItem> all = paperItemService.findAllP(map);
        //添加分页信息
        PageInfo<PaperItem> paperItemPageInfo = new PageInfo<>(all);
        return new ResponseEntity(paperItemPageInfo);
    }

    @GetMapping("/testSaveToRedis/{id}")
    public ResponseEntity testSaveToRedis(@PathVariable Integer id){
        paperItemService.savePaperToRedis(id);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping("/getRedisExam")
    public ResponseEntity getRedisExam(@RequestBody Map<String,Object> postMap){
        String redisExam = paperItemService.getRedisExam(postMap);
        return new ResponseEntity(redisExam);
    }

    //删除试卷
    @DeleteMapping("/deletePbyid/{id}")
    public ResponseEntity deletePbyid(@PathVariable int id){
        System.out.println(id);
        return paperItemService.deletePbyId(id);
    }
    //添加试卷
    @RequestMapping("addPaper")
    public ResponseEntity addPaper(@RequestBody PaperItem paperItem){
        if(paperItemService.addPaper(paperItem)){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.ERROR;
    }
    //改变试卷状态
    @RequestMapping("changePs")
    public ResponseEntity changePstatus(@RequestBody PaperItem paperItem){
        if(paperItemService.changePstatus(paperItem)){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.ERROR;
    }

}
